﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace System.Web
{
    public class AspxHeaderData
    {
        public readonly static string aspxHeader = "<%@\\s+(Page|Control)\\s+Language\\s*=\\s*\"([^\"]+)\"\\s+AutoEventWireup\\s*=\\s*\"([^\"]+)\"\\s+(CodeBehind|CodeFile)\\s*=\\s*\"([^\"]+)\"\\s+Inherits\\s*=\\s*\"([^\"]+)\"\\s+%>";
        public readonly static Regex aspxHeaderRegex = new Regex(aspxHeader);
        public AspxHeaderData(string aspxFileName)
        {
            string aspxFileContent = File.ReadAllText(aspxFileName);
            Match aspxHeaderMatch = aspxHeaderRegex.Match(aspxFileContent);
            if (aspxHeaderMatch.Success)
            {
                this.Page = aspxHeaderMatch.Groups[1].Value == "Page";
                this.Control = aspxHeaderMatch.Groups[1].Value == "Control";
                this.Language = aspxHeaderMatch.Groups[2].Value;
                this.AutoEventWireup = aspxHeaderMatch.Groups[3].Value == "true";
                this.IsCodeBehind = aspxHeaderMatch.Groups[4].Value == "CodeBehind";
                this.CodeBehind = this.IsCodeBehind ? aspxHeaderMatch.Groups[5].Value : null;
                this.IsCodeFile = aspxHeaderMatch.Groups[4].Value == "CodeFile";
                this.CodeFile = this.IsCodeFile ? aspxHeaderMatch.Groups[5].Value : null;
                this.Inherits = aspxHeaderMatch.Groups[6].Value;
            }
            else
            {
                throw new FileLoadException($"{aspxFileName}内容无法解析！");
            }
        }
        public bool Page { get; set; }
        public bool Control { get; set; }
        public string Language { get; set; }
        public bool AutoEventWireup { get; set; }
        public bool IsCodeBehind { get; set; }
        public bool IsCodeFile { get; set; }
        public string CodeBehind { get; set; }
        public string CodeFile { get; set; }
        public string Inherits { get; set; }
    }
}
